﻿package ahxu.crawler;

import java.util.Date;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class DefaultCrawler implements ICrawler {

	private HttpClient httpClient;
	private HttpContext context;
	private HttpGet httpget;

	public DefaultCrawler(HttpClient httpClient) {
		this.httpClient = httpClient;
		this.context = new BasicHttpContext();
	}

	public CrawlerResult doCrawl(UriInput input) throws CrawException {
		CrawlerResult ret = null;
		try {
			this.httpget = new HttpGet(input.getUri());
			Date startTime=new Date();
			HttpResponse response = this.httpClient.execute(httpget, this.context);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				
				String charset=EntityUtils.getContentCharSet(entity);
				String contentType=entity.getContentType()!=null?entity.getContentType().getValue():null;
				String encoding=entity.getContentEncoding()!=null?entity.getContentEncoding().getValue():null;
				byte[] source = EntityUtils.toByteArray(entity);
				entity.consumeContent();
				Date endTime=new Date();
				// create return
				ret = new CrawlerResult();
				ret.setAnthorText(input.getAnchorText());
				ret.setSource(source);
				ret.setSourceContentType(contentType);
				ret.setSourceContentEncoding(encoding);
				ret.setSourceCountCharset(charset);
				ret.setStartTime(startTime);
				ret.setEndTime(endTime);
				ret.setUri(input.getUri());
				ret.setId(UUID.randomUUID().toString());
			}
		} catch (Exception e) {
			this.httpget.abort();
			throw new CrawException(e);
		}
		return ret;
	}

}
